package testcasetool.transformation.uml.utp;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import proyecto.testcasetool.Parameter;
import proyecto.testcasetool.SingularInvalidValue;
import proyecto.testcasetool.SingularValidValue;
import testcasetool.transformation.testdata.ParameterCondition;
import testcasetool.transformation.testdata.ParameterConditionParser;
import testcasetool.transformation.testdata.ValueGeneratorCondition;

public class Parameter_DataPool {
	
	private static final String SEPARATOR = ", ";
	private Parameter parameter;
	private boolean validity;
	private ParameterCondition parameterCondition;

	public Parameter_DataPool(Parameter parameter, boolean validity) {
		super();
		this.parameter = parameter;
		this.validity = validity;
		this.parameterCondition = ParameterConditionParser.parseCondition(parameter);
	}
	
	public String getParameterData() {
		StringBuffer sb = new StringBuffer();
		if (hasToReturnValidValues()) {
			List<SingularValidValue> validos = this.parameter.getSingularValidValue();
			sb.append(this.parameter.getName() + ": (" + this.parameter.getCondition() + ") ");
			if (validos.size() > 0) {
				sb.append("| Valores: {");
				for (SingularValidValue value : validos) {
					sb.append(" " + value.getValue());
				}
				sb.append(" }");
			}
		} else {
			List<SingularInvalidValue> invalidos = this.parameter.getSingularInvalidValue();
			sb.append(this.parameter.getName() + ": NOT(" + this.parameter.getCondition() + ") ");
			if (invalidos.size() > 0) {
				sb.append("| Valores: {");
				for (SingularInvalidValue value : invalidos) {
					sb.append(" " + value.getValue());
				}
				sb.append(" }");
			}
		}
		return sb.toString();
	}
	
	public String toString() {
		return this.getParameterData();
	}
	
	public String getName() {
		return this.parameter.getName();
	}
	
	public String getType() {
		return this.parameter.getType();
	}
	
	public String getCondition() {
		return this.parameterCondition.getCondition(hasToReturnValidValues());
	}
	
	public String getGeneratedTestValues() {
		StringBuffer sb = new StringBuffer();
		if (this.parameterCondition instanceof ValueGeneratorCondition) {
			((ValueGeneratorCondition)this.parameterCondition).generateValues();
			if (hasToReturnValidValues()) {
				sb.append(StringUtils.join(((ValueGeneratorCondition)this.parameterCondition).getValidValues(), SEPARATOR));
			} else {
				sb.append(StringUtils.join(((ValueGeneratorCondition)this.parameterCondition).getInvalidValues(), SEPARATOR));
			}
		}
		return sb.toString();
	}
	public String getSingularTestValues() {
		StringBuffer sb = new StringBuffer();
		if (hasToReturnValidValues()) {
			sb.append(StringUtils.join(this.parameter.getSingularValidValue(),SEPARATOR));
		} else {
			sb.append(StringUtils.join(this.parameter.getSingularInvalidValue(),SEPARATOR));
		}
		return sb.toString();
	}

	private boolean hasToReturnValidValues() {
		return this.validity;
	}

	public String getDataType() {
		return this.parameter.getDatatype();
	}

	public String getDescription() {
		return this.parameter.getDescription();
	}

}
